package cn.qqtheme.framework.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.qqtheme.framework.drawable.StateColorDrawable;
import cn.qqtheme.framework.entity.FileItem;
import cn.qqtheme.framework.icons.FilePickerIcon;
import cn.qqtheme.framework.util.CompatUtils;
import cn.qqtheme.framework.util.ConvertUtils;
import cn.qqtheme.framework.util.FileUtils;
import cn.qqtheme.framework.util.LogUtils;
import java.io.File;
import java.util.ArrayList;
import net.lingala.zip4j.util.InternalZipConstants;

/* loaded from: classes.dex */
public class FileAdapter extends BaseAdapter {
    public static final String DIR_PARENT = "..";
    public static final String DIR_ROOT = ".";
    private ArrayList<FileItem> data = new ArrayList<>();
    private String rootPath = null;
    private String currentPath = null;
    private String[] allowExtensions = null;
    private boolean onlyListDir = false;
    private boolean showHomeDir = false;
    private boolean showUpDir = true;
    private boolean showHideDir = true;
    private int itemHeight = 40;
    private Drawable homeIcon = null;
    private Drawable upIcon = null;
    private Drawable folderIcon = null;
    private Drawable fileIcon = null;

    @Override // android.widget.Adapter
    public long getItemId(int i) {
        return i;
    }

    public String getCurrentPath() {
        return this.currentPath;
    }

    public void setFileIcon(Drawable drawable) {
        this.fileIcon = drawable;
    }

    public void setFolderIcon(Drawable drawable) {
        this.folderIcon = drawable;
    }

    public void setHomeIcon(Drawable drawable) {
        this.homeIcon = drawable;
    }

    public void setUpIcon(Drawable drawable) {
        this.upIcon = drawable;
    }

    public void setAllowExtensions(String[] strArr) {
        this.allowExtensions = strArr;
    }

    public void setOnlyListDir(boolean z) {
        this.onlyListDir = z;
    }

    public boolean isOnlyListDir() {
        return this.onlyListDir;
    }

    public void setShowHomeDir(boolean z) {
        this.showHomeDir = z;
    }

    public boolean isShowHomeDir() {
        return this.showHomeDir;
    }

    public void setShowUpDir(boolean z) {
        this.showUpDir = z;
    }

    public boolean isShowUpDir() {
        return this.showUpDir;
    }

    public void setShowHideDir(boolean z) {
        this.showHideDir = z;
    }

    public boolean isShowHideDir() {
        return this.showHideDir;
    }

    public void setItemHeight(int i) {
        this.itemHeight = i;
    }

    public void loadData(String str) {
        File[] listDirsAndFiles;
        if (str == null) {
            LogUtils.warn("current directory is null");
            return;
        }
        if (this.homeIcon == null) {
            this.homeIcon = ConvertUtils.toDrawable(FilePickerIcon.getHOME());
        }
        if (this.upIcon == null) {
            this.upIcon = ConvertUtils.toDrawable(FilePickerIcon.getUPDIR());
        }
        if (this.folderIcon == null) {
            this.folderIcon = ConvertUtils.toDrawable(FilePickerIcon.getFOLDER());
        }
        if (this.fileIcon == null) {
            this.fileIcon = ConvertUtils.toDrawable(FilePickerIcon.getFILE());
        }
        ArrayList arrayList = new ArrayList();
        if (this.rootPath == null) {
            this.rootPath = str;
        }
        LogUtils.verbose("current directory path: " + str);
        this.currentPath = str;
        if (this.showHomeDir) {
            FileItem fileItem = new FileItem();
            fileItem.setDirectory(true);
            fileItem.setIcon(this.homeIcon);
            fileItem.setName(DIR_ROOT);
            fileItem.setSize(0L);
            fileItem.setPath(this.rootPath);
            arrayList.add(fileItem);
        }
        if (this.showUpDir && !str.equals(InternalZipConstants.ZIP_FILE_SEPARATOR)) {
            FileItem fileItem2 = new FileItem();
            fileItem2.setDirectory(true);
            fileItem2.setIcon(this.upIcon);
            fileItem2.setName(DIR_PARENT);
            fileItem2.setSize(0L);
            fileItem2.setPath(new File(str).getParent());
            arrayList.add(fileItem2);
        }
        String[] strArr = this.allowExtensions;
        if (strArr == null) {
            if (this.onlyListDir) {
                listDirsAndFiles = FileUtils.listDirs(this.currentPath);
            } else {
                listDirsAndFiles = FileUtils.listDirsAndFiles(this.currentPath);
            }
        } else if (this.onlyListDir) {
            listDirsAndFiles = FileUtils.listDirs(this.currentPath, strArr);
        } else {
            listDirsAndFiles = FileUtils.listDirsAndFiles(this.currentPath, strArr);
        }
        if (listDirsAndFiles != null) {
            for (File file : listDirsAndFiles) {
                if (this.showHideDir || !file.getName().startsWith(DIR_ROOT)) {
                    FileItem fileItem3 = new FileItem();
                    boolean isDirectory = file.isDirectory();
                    fileItem3.setDirectory(isDirectory);
                    if (isDirectory) {
                        fileItem3.setIcon(this.folderIcon);
                        fileItem3.setSize(0L);
                    } else {
                        fileItem3.setIcon(this.fileIcon);
                        fileItem3.setSize(file.length());
                    }
                    fileItem3.setName(file.getName());
                    fileItem3.setPath(file.getAbsolutePath());
                    arrayList.add(fileItem3);
                }
            }
        }
        this.data.clear();
        this.data.addAll(arrayList);
        notifyDataSetChanged();
    }

    public void recycleData() {
        Bitmap bitmap;
        Bitmap bitmap2;
        Bitmap bitmap3;
        Bitmap bitmap4;
        this.data.clear();
        Drawable drawable = this.homeIcon;
        if ((drawable instanceof BitmapDrawable) && (bitmap4 = ((BitmapDrawable) drawable).getBitmap()) != null && !bitmap4.isRecycled()) {
            bitmap4.recycle();
        }
        Drawable drawable2 = this.upIcon;
        if ((drawable2 instanceof BitmapDrawable) && (bitmap3 = ((BitmapDrawable) drawable2).getBitmap()) != null && !bitmap3.isRecycled()) {
            bitmap3.recycle();
        }
        Drawable drawable3 = this.folderIcon;
        if ((drawable3 instanceof BitmapDrawable) && (bitmap2 = ((BitmapDrawable) drawable3).getBitmap()) != null && !bitmap2.isRecycled()) {
            bitmap2.recycle();
        }
        Drawable drawable4 = this.fileIcon;
        if (!(drawable4 instanceof BitmapDrawable) || (bitmap = ((BitmapDrawable) drawable4).getBitmap()) == null || bitmap.isRecycled()) {
            return;
        }
        bitmap.recycle();
    }

    @Override // android.widget.Adapter
    public int getCount() {
        return this.data.size();
    }

    @Override // android.widget.Adapter
    public FileItem getItem(int i) {
        return this.data.get(i);
    }

    @Override // android.widget.Adapter
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        LinearLayout linearLayout;
        Context context = viewGroup.getContext();
        if (view == null) {
            LinearLayout linearLayout2 = new LinearLayout(context);
            CompatUtils.setBackground(linearLayout2, new StateColorDrawable(-1, -3355444));
            linearLayout2.setOrientation(0);
            linearLayout2.setGravity(16);
            int px = ConvertUtils.toPx(context, this.itemHeight);
            if (viewGroup instanceof AbsListView) {
                linearLayout2.setLayoutParams(new AbsListView.LayoutParams(-1, px));
            } else {
                linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(-1, px));
            }
            int px2 = ConvertUtils.toPx(context, 5.0f);
            linearLayout2.setPadding(px2, px2, px2, px2);
            ImageView imageView = new ImageView(context);
            int px3 = ConvertUtils.toPx(context, 30.0f);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(px3, px3));
            imageView.setImageResource(17301579);
            linearLayout2.addView(imageView);
            TextView textView = new TextView(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, -1);
            layoutParams.leftMargin = ConvertUtils.toPx(context, 10.0f);
            textView.setLayoutParams(layoutParams);
            textView.setGravity(8388627);
            textView.setSingleLine();
            linearLayout2.addView(textView);
            viewHolder = new ViewHolder();
            viewHolder.imageView = imageView;
            viewHolder.textView = textView;
            linearLayout2.setTag(viewHolder);
            linearLayout = linearLayout2;
        } else {
            viewHolder = (ViewHolder) view.getTag();
            linearLayout = view;
        }
        FileItem fileItem = this.data.get(i);
        viewHolder.imageView.setImageDrawable(fileItem.getIcon());
        viewHolder.textView.setText(fileItem.getName());
        return linearLayout;
    }

    /* loaded from: classes.dex */
    private static class ViewHolder {
        ImageView imageView;
        TextView textView;

        private ViewHolder() {
        }
    }
}
